<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Control Structures</em>
</p>
<p class='vspace'></p><h3>Switch (case) Statement, used with sensor input</h3>
<p>Switch statements allow mulitple choices in which sections of code to run for a given conditional.
</p>
<p class='vspace'></p><h3>Hardware Required</h3>
<ul><li>MSP430 LaunchPad
</li><li>(1) photocell, or analog sensor
</li><li>(1) 10k ohm resistors
</li><li>breadboard
</li><li>hook-up wire
</li></ul><p class='vspace'></p>
<h3>Relevant Groundwork</h3>
<p>An if statement allows you to choose between two discrete options, TRUE or FALSE.  When there are more than two options, you can use multiple if statements, or you can use the <a class='wikilink' href='SwitchCase.html'><strong>switch</strong></a> statement.  Switch allows you to choose between several discrete options.  This tutorial shows you how to use it to switch between four desired states of a photo resistor:  really dark, dim, medium, and bright.
</p>
<p class='vspace'></p><p>This program first reads the photoresistor.  Then it uses the <code>map()</code> function to map its output to one of four values: 0, 1, 2, or 3.  Finally, it uses the <code>switch()</code> statement to print one of four messages back to the computer depending on which of the four values is returned. 
</p>
<p>The photoresistor is connected to analog in pin A3 using a voltage divider circuit.  A 10Kilohm resistor makes up the other side of the voltage divider, running from analog in A3 to ground.  The <code>analogRead()</code> function returns a range of about 0 to 600 from this circuit in a reasonably lit indoor space.
</p>
<p class='vspace'></p><h3>Circuit</h3>
<p class='vspace'></p><div><img height='400px' src='../reference/img/SwitchCaseTutorial_bb.png' alt='' title='' /></div>
<p class='vspace'></p><p><span style='font-size:83%'>image developed using <a class='urllink' href='http://www.fritzing.org' rel='nofollow'>Fritzing</a>. For more circuit examples, see the <a class='urllink' href='http://fritzing.org/projects/' rel='nofollow'>Fritzing project page</a> </span>
</p>
<p class='vspace'></p><p><h3>Schematic:</h3>
</p>
<p class='vspace'></p><div><img width='400px' src='../reference/img/SwitchCaseTutorial_schem.png' alt='' title='' /></div>
<p class='vspace'></p><h3>Code Explanation</h3>
<p>None.
</p>
<p class='vspace'></p><h3>Code</h3>
<pre>
/*
  Switch statement
 
  Demonstrates the use of a switch statement.  The switch
  statement allows you to choose from among a set of discrete values
  of a variable.  It's like a series of if statements.
 
  To see this sketch in action, but the board and sensor in a well-lit
  room, open the serial monitor, and and move your hand gradually
  down over the sensor.
 
  The circuit:
  * photoresistor from analog in 0 to +3V
  * 10K resistor from analog in 0 to ground
 
  created 1 Jul 2009
  by Tom Igoe 
  modified 30 Apr 2013
  by Adrian Fernandez 
 
  This example code is in the public domain.
 
*/

// these constants won't change. They are the
// lowest and highest readings you get from your sensor:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 600;    // sensor maximum, discovered through experiment

void setup() {
  // initialize serial communication:
  Serial.begin(9600);  
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A3);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the 
  // range value:
  switch (range) {
  case 0:    // your hand is on the sensor
    Serial.println("dark");
    break;
  case 1:    // your hand is close to the sensor
    Serial.println("dim");
    break;
  case 2:    // your hand is a few inches from the sensor
    Serial.println("medium");
    break;
  case 3:    // your hand is nowhere near the sensor
    Serial.println("bright");
    break;
  } 
  delay(1);        // delay in between reads for stability
}
</pre>
<h3>Working Video</h3>
(Insert Video Here)
<h3>Try it out:</h3>
- use a switch case to integrate with a button
<p class='vspace'></p><h3>See Also:</h3>
<ul><li><a class='wikilink' href='Serial_Begin.html'>serial.begin()</a>
</li><li><a class='wikilink' href='AnalogRead.html'>analogRead()</a>
</li><li><a class='wikilink' href='Map.html'>map()</a>
</li><li><a class='wikilink' href='Serial_Print.html'>Serial.println()</a>
</li><li><a class='wikilink' href='Tutorial_IfStatement.html'>If Statement</a> - how to use an if statement to change output conditions based on changing input conditions.
</li><li><a class='wikilink' href='Tutorial_ForLoop.html'>For Loop</a>: - controlling multiple <span class='wikiword'>LEDs</span> with a for loop.
</li><li><a class='wikilink' href='Tutorial_Array.html'>Array</a>: a variation on the For Loop example that demonstrates how to use an array.
</li><li><a class='wikilink' href='Tutorial_WhileLoop.html'>While Loop</a>: how to use a while loop to calibrate a sensor while a button is being read.
</li></ul>
<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
